package user

import (
	"github.com/goflyfox/gtoken/gtoken"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"ticket/utils"
	"time"
)

func LoginAfter(r *ghttp.Request, respData gtoken.Resp) {
	// 获取用户名
	username := respData.Get("userKey")
	token := respData.Get("token").Val().(string)

	// 用户验证成功，查询用户详情
	var userAgents utils.UserAgents

	// 根据用户名查询ID
	g.Model("user_agents").
		Fields("user_agents.*, agents.name AS agentName").
		InnerJoin("agents on agents.id = user_agents.agent_id").
		Where("user_agents.username = ?", username).Scan(&userAgents)

	// 用户登录成功就设置用户信息【设置6小时有效期】
	utils.SetUserInfo("Bearer "+token, userAgents, time.Hour*1)

	r.Response.WriteJsonExit(g.Map{
		"code": 0,
		"msg":  "success",
		"data": g.Map{
			"token": token,
		},
	})
}
